package me.luger.project.base.modules.sys.controller.api;

import me.luger.core.aspect.log.OpLog;
import me.luger.core.auth.cookie.util.CookieAuthUtil;
import me.luger.core.commom.util.LogUtil;
import me.luger.core.mvc.controller.BaseController;
import me.luger.core.mvc.entity.Result;
import me.luger.core.mvc.filter.pagecache.annotation.PageCache;
import org.apache.shiro.codec.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.ResponseBody;

import me.luger.project.base.modules.sys.service.AdminUserService;
import me.luger.project.base.modules.sys.entity.AdminUser;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.AlgorithmParameters;
import java.security.Security;
import java.util.Arrays;
import java.util.List;

/**
 * 系统管理员 controller
 */
@Controller
@RequestMapping("/api/sys/adminUser")
public class AdminUserApiController extends BaseController {

    @Autowired
    private AdminUserService adminUserService;

    @RequestMapping(value = "/login",name = "接口描述")
    @ResponseBody
    public Object login(){
        try{
            CookieAuthUtil.setAuthCookie(request,response,"12345");
        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return Result.error();
        }
        return Result.success();
    }

    @RequestMapping(value = "/demo",name = "接口描述")
    @ResponseBody
    public Object demo(String id){

        return Result.success(id);
    }

    @RequestMapping(value = "/test/{orderId}/{tid}",name = "接口描述")
    @ResponseBody
    @OpLog(name = "测试",detail = "测试哈哈#{#orderId},tid=#{#tid}")
    public Object test(@PathVariable(name = "tid") Integer tid1,
                       @PathVariable(name = "orderId") String orderId1,
                       @RequestParam(defaultValue = "1") Integer pn,
                       @RequestParam(defaultValue = "50") Integer rn){
        try{

        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return Result.error();
        }
        return Result.success();
    }

    @RequestMapping(value = "/jsonView",name = "接口描述")
    @PageCache
    public ModelAndView jsonView(){
        try{
            System.err.println("-----------------luger-------------------");
            System.out.println("-----------------luger-------------------");
        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return jsonView(Result.info().addDateItem("a","1").addDateItem("b","2"));
        }
        return jsonView(Result.info().addDateItem("a","1").addDateItem("b","2"));
    }

    @RequestMapping(value = "/jsonView10",name = "接口描述")
    @PageCache(timeToLiveSeconds = 10)
    public ModelAndView jsonView10(){
        try{
            System.err.println("-----------------luger-------------------");
            System.out.println("-----------------luger-------------------");
        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return jsonView(Result.info().addDateItem("a","1").addDateItem("b","2"));
        }
        return jsonView(Result.info().addDateItem("a","1").addDateItem("b","2"));
    }

    @RequestMapping(value = "/jsonView20",name = "接口描述")
    @PageCache(timeToLiveSeconds = 20)
    public ModelAndView jsonView20(){
        try{
            System.err.println("-----------------luger-------------------");
            System.out.println("-----------------luger-------------------");
        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return jsonView(Result.info().addDateItem("a","1").addDateItem("b","2"));
        }
        return jsonView(Result.info().addDateItem("a","1").addDateItem("b","2"));
    }

    @RequestMapping(value = "/xmlView",name = "接口描述")
    public ModelAndView xmlView(){
        try{

        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return xmlView(Result.info().addDateItem("a","1").addDateItem("b","2"));
        }
        return xmlView(Result.info().addDateItem("a","1").addDateItem("b","2"));
    }


    public static String getUserInfo(String encryptedData, String sessionKey, String iv){
        // 被加密的数据
        byte[] dataByte = Base64.decode(encryptedData);
        // 加密秘钥
        byte[] keyByte = Base64.decode(sessionKey);
        // 偏移量
        byte[] ivByte = Base64.decode(iv);

        try {
            // 如果密钥不足16位，那么就补足.  这个if 中的内容很重要
            int base = 16;
            if (keyByte.length % base != 0) {
                int groups = keyByte.length / base + (keyByte.length % base != 0 ? 1 : 0);
                byte[] temp = new byte[groups * base];
                Arrays.fill(temp, (byte) 0);
                System.arraycopy(keyByte, 0, temp, 0, keyByte.length);
                keyByte = temp;
            }
            // 初始化
            Security.addProvider(new BouncyCastleProvider());
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding","BC");
            SecretKeySpec spec = new SecretKeySpec(keyByte, "AES");
            AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");
            parameters.init(new IvParameterSpec(ivByte));
            cipher.init(Cipher.DECRYPT_MODE, spec, parameters);// 初始化
            byte[] resultByte = cipher.doFinal(dataByte);
            if (null != resultByte && resultByte.length > 0) {
                String result = new String(resultByte, "UTF-8");
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
